﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sis.Entities;
using Sis.Service;
using Microsoft.Practices.Unity;

namespace Sis.Web.Controllers
{
    public class PromotorController : Controller
    {
        private readonly IPromotorService _promotorService;
        private readonly IIslaVentaService _islaVentaService;
        private readonly IUbigeoService _ubigeoService;


        public PromotorController(IPromotorService promotorService,
            IIslaVentaService islaVentaService, IUbigeoService ubigeoService)
        {
            _promotorService = promotorService;
            _islaVentaService = islaVentaService;
            _ubigeoService = ubigeoService;

        }
        //public PromotorController(){}

        public ActionResult Index()
        {
            var lista = _promotorService.GetAllFromPromotores("");
            return View(lista);
        }
        [HttpPost]
        public JsonResult AutoCompleteCountry(string query)
        {
            var result = _ubigeoService.GetallFromUbigeosByCriterio(query);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Index(string criterio)
        {
            var lista = _promotorService.GetAllFromPromotores(criterio);

            return View(lista);
        }
        //
        // GET: /Promotor/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Promotor/Create

        public ActionResult Create()
        {
            ViewData["IdIslaVenta"] = new SelectList(_islaVentaService.GetAllFromIslaVentas().ToList(), "IdIslaVenta", "Nombre");
            IEnumerable<SelectListItem> items = _islaVentaService.GetAllFromIslaVentas().Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text = c.Nombre
            });


            ViewData["IdIslaVenta"] = items;

            return View("Create");
        }

        //
        // POST: /Promotor/Create

        [HttpPost]
        public ActionResult Create(Promotor promotor)
        {

            if (ModelState.IsValid)
            {

                _promotorService.AddPromotor(promotor);
                return RedirectToAction("Index");

            }
            else
            {
                ViewData["IdIslaVenta"] = new SelectList(_islaVentaService.GetAllFromIslaVentas().ToList(), "IdIslaVenta", "NombreIsla");
                IEnumerable<SelectListItem> items = _islaVentaService.GetAllFromIslaVentas().Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Nombre
                });


                ViewData["IdIslaVenta"] = items;


                return View("Create", promotor);
            }
        }

        //
        // GET: /Promotor/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Promotor/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Promotor/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Promotor/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
